package com.xisoft.generator.utils;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.xisoft.base.entity.BaseVO;
import com.xisoft.base.entity.ReferEntity;
import com.xisoft.base.service.XfServiceImpl;
import com.xisoft.common.db.SqlHelper;
import com.xisoft.common.bean.BeanHelper;
import com.xisoft.common.bean.ReflectionUtil;
import com.xisoft.common.io.StreamUtil;
import com.xisoft.common.pubEnv.JsonUtil;
import com.xisoft.common.pubEnv.PubEnvUtil;
import com.xisoft.common.pubEnv.StringUtil;
import com.xisoft.devise.entity.salary.SalaryDataEntity;
import com.xisoft.devise.service.impl.SalaryDataImpl;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

/**
 * *
 * Created with Description:
 *
 * @version 1.0 <br>
 * @packagename: com.xisoft.utils<br>
 * @author: LiBencheng <br>
 * @computer: licheng <br>
 * @date: Created on 2022/8/9 16:43 <br>
 */
public class TestDemo
{
    @Test
    public void test07( ) throws Exception
    {
        String str = "{tableName:sys_organization, fields: ocode AS code,oname AS name ,pk:oid}";
        Map< String,Object > toMap = JsonUtil.toMap(str);
        System.out.println(toMap);
        ReferEntity refer = new XfServiceImpl<>().getBhelp().parseVO(toMap, ReferEntity.class);
        System.out.println(refer);
        System.out.println(refer.getRefSql());

    }

    @Test
    public void test06( ) throws Exception
    {
        String str = "{classpatch:'com.xisoft.devise.entity.salary.SalaryDataEntity', id:'111111,22222'}";
        Map< String,Object > toMap = JsonUtil.toMap(str);
        System.out.println(toMap);
        BaseVO baseEntity = ReflectionUtil.newInstance(StringUtil.toString(toMap.get("classpatch")));
        System.out.println(toMap);
        System.out.println(baseEntity.getChildEntity());
        List< String > ids = Arrays.asList(StringUtil.toString(toMap.get("id")).split(PubEnvUtil.COMMA));
        String selectSQL = SqlHelper.getSelectSQL(baseEntity.getTableName()
                , new String[]{baseEntity.getPrimaryKey()}
                , SqlHelper.getPkInSQLByCol(baseEntity.getPrimaryKey(), ids), null);
        System.out.println(selectSQL);
        String deleteSQL = SqlHelper.getDeleteSQL(baseEntity.getTableName(),
                                                  SqlHelper.getPkInSQLByCol(baseEntity.getPrimaryKey(), ids));
        System.out.println(deleteSQL);

        Map< String,String > childMap = baseEntity.getChildEntity();
        for (String tableCode : childMap.keySet())
        {
            BaseVO childEntity = ReflectionUtil.newInstance(childMap.get(tableCode));
            System.out.println(childEntity.getChildEntity());
            deleteSQL = SqlHelper.getDeleteSQL(childEntity.getTableName()
                    , baseEntity.getPrimaryKey() + " IN (" + selectSQL + ")");
            System.out.println(deleteSQL);
        }


    }

    @Test
    public void test05( ) throws Exception
    {
        String filePath = this.getClass().getResource("/config/salarydata.json").getPath();
        String str = new String(StreamUtil.readFileToByteArray(filePath), PubEnvUtil.UTF_8);
        List< Map< String,Object > > jsonObj = JsonUtil.toList(str);
        SalaryDataEntity fatherVO = new XfServiceImpl<>().getBhelp().parseVO(jsonObj, SalaryDataEntity.class);
        List aggEntity = new SalaryDataImpl().getBeanAggEntity(SalaryDataEntity.class.getName(), jsonObj);
    }

    @Test
    public void test04( ) throws Exception
    {
        //开辟空间
        HashMap< Integer,ArrayList > hashMap = new HashMap< Integer,ArrayList >();
        //存入数据
        hashMap.put(1, new ArrayList< String >()
        {{ add("张三"); }});
        hashMap.put(2, new ArrayList< String >()
        {{ add("李四"); }});
        hashMap.put(3, new ArrayList< String >()
        {{ add("王五"); }});

        //使用Collection类型接收HashMap的Value值
        Collection< ArrayList > collection = hashMap.values();
        //把Collection对象作为参数传入ArrayList构造方法完成类型转换
        ArrayList< ArrayList > arrayList = new ArrayList< ArrayList >(collection);
        //输出测试
        System.out.println(hashMap.keySet().stream().collect(Collectors.toList()));
        Optional o = hashMap.values().stream().reduce(new BinaryOperator< ArrayList >()
        {
            @Override public ArrayList apply(ArrayList arrayList, ArrayList arrayList2)
            {
                arrayList.addAll(arrayList2);
                return arrayList;
            }
        });

        // hashMap.values().stream().collect( (Supplier) ArrayList::new, List::add,
        //                            (left, right) -> { left.addAll(right); return left; });
        System.out.println(o.get());
        System.out.println(arrayList.toString());

    }

    @Test
    public void test03( ) throws Exception
    {
        Object newInstance = ReflectionUtil.newInstance("com.xisoft.devise.entity.SalaryDataDetailsEntity");
        System.out.println(newInstance);
        newInstance = ReflectionUtil.newInstance("com.xisoft.devise.entity.SalaryDataEntity");
        System.out.println(newInstance);
    }

    @Test
    public void test02( ) throws Exception
    {
        SalaryDataEntity obj = new SalaryDataEntity();
        String selectSQL = SqlHelper.getSelectSQL(SalaryDataEntity.TABLE_NAME, null, "1=1", "");
        selectSQL = "SELECT * FROM HR_SALARY_DATA WHERE SALARY_DATA_ID='yydsbc1d73776a9040289275eed7ed6113a0'";
        System.out.println(selectSQL);
        List< Entity > list = Db.use().query(selectSQL);
        System.out.println(list);
        System.out.println(BeanHelper.transBean2Map(obj));

    }

    @Test
    public void test01( )
    {
        String json = "{table:{name:HR_SALARY_DATA,child:[{table:{name:HR_SALARY_DATA_DETAILS}}]}}";
        Map< String,Object > map = JsonUtil.toMap(json);
        System.out.println(map);

        json = "[{table:{name:HR_SALARY_DATA}},{table:{name:HR_SALARY_DATA_DETAILS}}]";
        List< Map< String,Object > > list = JsonUtil.toList(json);
        System.out.println(list);
        json = "[{'table':{'name':'HR_SALARY_DATA','child':'11'}},{'table':{'name':'HR_SALARY_DATA_DETAILS'}}]";
        list = JsonUtil.toList(json);
        System.out.println(list);
        // json = "['11','22','33','44']";
        // list = JsonUtil.toList(json);
        // System.out.println(list);
        System.out.println("..........................");

        String tableName = "hr_salary_data_details";
        System.out.println(tableName);
        System.out.println(StringUtil.getFuncCodeFromResID(tableName));
        System.out.println(StringUtil.getBirthdate("510824199209274252"));
        String[] strs = tableName.split(PubEnvUtil.UNDERSCORE);
        for (int i = 0; i < PubEnvUtil.getSize(strs); i++)
        {
            System.out.println(StringUtil.firstCharUpperCase(strs[i]));
        }

    }
}
